package com.xkdx.guangguang.sql;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.xkdx.guangguang.shareclass.BrandDetail;
import com.xkdx.guangguang.shareclass.Shop;

public class AttentionDataHelper {

	private String TABNAME = "attention";
	private AttentionSQLiteHelper dbHelper;
	private SQLiteDatabase db;
	private static int DB_VERSION = 2;
	boolean shopInsert = true;
	boolean brandInsert = true;
	private String SHOPID = "shopID";
	private String BRANDID = "brandID";
	private String SHOPURL = "shopUrl";
	private String BRANDURL = "brandUrl";
	private String SHOPNAME = "shopName";
	private String BRANDNAME = "brandName";
	private String SHOPTYPE = "shopType";
	private String TAG = "tag";

	public AttentionDataHelper(Context context) {
		dbHelper = new AttentionSQLiteHelper(context, TABNAME, null, DB_VERSION);
	}

	public void Close() {
		db.close();
		dbHelper.close();
	}

	public void insertShop(Shop shop) {
		if (shop == null) {
			return;
		}

		try {
			db = dbHelper.getWritableDatabase();
			Cursor cursor = db.query(TABNAME, null, null, null, null, null, null);
			if (cursor != null) {
				while (cursor.moveToNext()) {
					if (shop.getShopID().equals(cursor.getString(cursor.getColumnIndex(SHOPID)))) {
						shopInsert = false;
					}
				}
				if (shopInsert) {
					ContentValues values = new ContentValues();
					values.put(SHOPNAME, shop.getShopName());
					values.put(SHOPID, shop.getShopID());
					values.put(SHOPURL, shop.getLogo());
					values.put(SHOPTYPE, shop.getShopType());
					values.put(TAG, "1");
					db.insert(TABNAME, null, values);
					Close();
				}
			} else {
				ContentValues values = new ContentValues();
				values.put(SHOPNAME, shop.getShopName());
				values.put(SHOPID, shop.getShopID());
				values.put(SHOPURL, shop.getLogo());
				values.put(SHOPTYPE, shop.getShopType());
				values.put(TAG, "1");
				db.insert(TABNAME, null, values);
				Close();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 删除商场
	public void deletedShop(Shop shop) {
		if (shop == null) {
			return;
		}

		try {
			db = dbHelper.getWritableDatabase();
			Cursor cursor = db.query(TABNAME, null, null, null, null, null, null);
			while (cursor.moveToNext()) {
				if (shop.getShopID().equals(cursor.getString(cursor.getColumnIndex(SHOPID)))) {
					db.delete(TABNAME, "shopID=?", new String[] { shop.getShopID() });

				}
			}
			cursor.close();
			db.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	// 商场是否已经关注
	public boolean isShopAttentioned(Shop shop) {
		if (shop == null) {
			return false;
		}
		try {

			db = dbHelper.getWritableDatabase();
			Cursor cursor = db.query(TABNAME, null, null, null, null, null, null);
			while (cursor.moveToNext()) {
				if (shop.getShopID().equals(cursor.getString(cursor.getColumnIndex(SHOPID)))) {
					db.close();
					cursor.close();
					return true;
				}
			}
			cursor.close();
			db.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	public void insertBrand(BrandDetail brandDetail) {
		if (brandDetail == null) {
			return;
		}
		try {
			db = dbHelper.getWritableDatabase();
			Cursor cursor = db.query(TABNAME, null, null, null, null, null, null);
			if (cursor != null) {
				while (cursor.moveToNext()) {
					if (String.valueOf(brandDetail.getBrandID()).equals(cursor.getString(cursor.getColumnIndex(BRANDID)))) {
						brandInsert = false;
					}
				}
				if (brandInsert) {
					db = dbHelper.getWritableDatabase();
					ContentValues values = new ContentValues();
					values.put(BRANDID, brandDetail.getBrandID());
					values.put(BRANDNAME, brandDetail.getBrandSimpleName());
					values.put(BRANDURL, brandDetail.getLogo());
					values.put(TAG, "2");
					db.insert(TABNAME, null, values);
					Close();
				}

			} else {
				db = dbHelper.getWritableDatabase();
				ContentValues values = new ContentValues();
				values.put(BRANDID, brandDetail.getBrandID());
				values.put(BRANDNAME, brandDetail.getBrandSimpleName());
				values.put(BRANDURL, brandDetail.getLogo());
				values.put(TAG, "2");
				db.insert(TABNAME, null, values);
				Close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// 品牌是否已经关注
	public boolean isBrandAttentioned(BrandDetail brandDetail) {
		if (brandDetail == null) {
			return false;
		}
		try {
			db = dbHelper.getWritableDatabase();
			Cursor cursor = db.query(TABNAME, null, null, null, null, null, null);
			while (cursor.moveToNext()) {
				if (String.valueOf(brandDetail.getBrandID()).equals(cursor.getString(cursor.getColumnIndex(BRANDID)))) {
					db.close();
					cursor.close();
					return true;
				}
			}
			db.close();
			cursor.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}

	public List<Shop> getAttentionShop() {
		List<Shop> list = new ArrayList<Shop>();
		try {
			db = dbHelper.getWritableDatabase();

			Cursor cursor = db.query(TABNAME, null, "tag=?", new String[] { "1" }, null, null, null);
			while (cursor.moveToNext()) {
				Shop shop = new Shop();
				shop.setShopID(cursor.getString(cursor.getColumnIndex(SHOPID)));
				shop.setShopName(cursor.getString(cursor.getColumnIndex(SHOPNAME)));
				shop.setLogo(cursor.getString(cursor.getColumnIndex(SHOPURL)));
				shop.setShopType(cursor.getString(cursor.getColumnIndex(SHOPTYPE)));
				list.add(shop);
			}
			cursor.close();
			Close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}

	public List<BrandDetail> getAttentionBrandInfo() {
		List<BrandDetail> list = new ArrayList<BrandDetail>();
		try {

			db = dbHelper.getWritableDatabase();

			Cursor cursor = db.query(TABNAME, null, "tag=?", new String[] { "2" }, null, null, null);
			while (cursor.moveToNext()) {
				BrandDetail brandDetail = new BrandDetail();
				brandDetail.setBrandID(Integer.valueOf(cursor.getString(cursor.getColumnIndex(BRANDID))));
				brandDetail.setBrandName(cursor.getString(cursor.getColumnIndex(BRANDNAME)));
				brandDetail.setLogo(cursor.getString(cursor.getColumnIndex(BRANDURL)));
				list.add(brandDetail);
			}
			cursor.close();
			Close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return list;
	}

	public void deletedBrand(BrandDetail brandDetail) {
		if (brandDetail == null) {
			return;
		}
		try {

			db = dbHelper.getWritableDatabase();
			Cursor cursor = db.query(TABNAME, null, null, null, null, null, null);
			while (cursor.moveToNext()) {
				if (String.valueOf(brandDetail.getBrandID()).equals(cursor.getString(cursor.getColumnIndex(BRANDID)))) {
					db.delete(TABNAME, "brandID=?", new String[] { String.valueOf(brandDetail.getBrandID()) });
				}
			}
			cursor.close();
			db.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

}
